use([
    "public/javascripts/service/bill_detailService.js",
    "public/javascripts/widget/slider/slider.js",
    "public/javascripts/widget/prompt/prompt.js",
    "public/javascripts/widget/calander/calander.js",
    "public/javascripts/widget/select/select.js"
    ], function(bill_detailService, SliderComponent, PromptComponent, Calander, selectFactory) {
    var PageComponent = React.createClass({
        componentDidMount: function() {
            Calander.init({
                ele: ['#beginDate', '#endDate'],
                isClear: true,
            });
            $("#beginDate").change(function(e){
                self.refs.startEffectTime.setValue(e.target.value);
            });
            $("#endDate").change(function(e){
                self.refs.endEffectTime.setValue(e.target.value);
            });
            this.primary = cookie.get('primary') == 'true' ? true : false;
            this.setState({'primary':this.primary});

            this.getSubAccount();
        },
        getInitialState: function() {
            return this.props.data ? this.props.data : {}
        },
        searchOrders: function(param) {
            if(!param) param = {};
            if(param.pageIndex) {
                this.extend(param, this.state.searchData);
            }
            if (this.state.userList && param.customerId) {
                this.state.userList.forEach(function (item) {
                    if (item.nameCode === param.customerId) param.customerId = item.id;
                });
            }

            param.customerId = param.customerId ? param.customerId : this.state.customerId;
            param.billNo = this.state.billNo;
            param.orderNo = param.orderNo ? param.orderNo.trim() : '';

            var _this = this;
            bill_detailService.searchOrders({ urlParams: param }, function(res) {
                if(res.errorCode == 0){
                    _this.setState({
                        list: res.data,
                        total: res.pagination.totalResult
                    });
                }
            });
        },
        extend : function(dist, source){
            for(var i in source) if(source[i]) dist[i] = source[i];
        },
        getSubAccount: function() {
            var _this = this;
            bill_detailService.getSubAccount('', function(res) {
                if(!res.data) return;
                _this.setState({ userList: res.data });
                selectFactory({
                    data: [],
                    id: "username",
                    isSearch: true,
                    pagination: true,
                    closeLocalSearch: true,
                    defaultText: '全部',
                    searchPlaceHoder: '请输入姓名或用户名',
                    showTextField: 'nameCode',
                    attrTextModel:function(fullName){
                        _this.refs.customerId.setValue(fullName);
                    },
                   onSearchValueChange: function(attachEvent, data, currentPage) {
                        bill_detailService.getSubAccount({ q: data, pageIndex: currentPage }, function (res) {
                            res.data.forEach(function(item){
                                item.nameCode = item.fullName + '(' + item.userName + ')';
                            });
                            _this.setState({ userList: res.data });
                            attachEvent.setData(res);
                        });
                    }
                });
            });
        },
        submit : function(data) {
            this.refs.form.setFormPristine();
            if (data.orderNo) {
                searchData = { orderNo: data.orderNo, q:data.q };
            } else {
                searchData = data;
            }
            searchData.isSearch = searchData.customerId === '' ? false : true;
            this.setState({ searchData: searchData, current: 1 });
            this.searchOrders(searchData);
        },
        resetForm : function() {
            this.refs.form.setFormPristine();
            this.refs.form.reset();
            this.setState({ searchData: '' });
            this.searchOrders();
        },
        onPageChange : function(pageIndex,pageSize){
            var param = {pageSize: pageSize, pageIndex:pageIndex};
            this.setState({ pageSize: pageSize, current: pageIndex });
            this.extend(param, this.state.searchData);
            this.searchOrders(param);
        },
        render:function() {
            var Browser = typeof window != 'undefined';
            var component = this;
            return ~init;
        }
    });
    if(typeof ReactDOM != "undefined") {
        ReactDOM.render(<PageComponent data={storage}/>, document.getElementById('container'));
    }
    module.exports = PageComponent;
});
